/*
		ServEngine framework library
		A library to fast develop JEE online applications
	
		Copyright 2008 José Ignacio de Córdoba Álvaro

		Licensed under the Apache License, Version 2.0 (the "License");
		you may not use this file except in compliance with the License.
		You may obtain a copy of the License at

				http://www.apache.org/licenses/LICENSE-2.0

		Unless required by applicable law or agreed to in writing, software
		distributed under the License is distributed on an "AS IS" BASIS,
		WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
		See the License for the specific language governing permissions and
		limitations under the License.
	
		Jose Ignacio de Cordoba Alvaro
		http://ignacio.decordoba.com
*/

package

com.servengine.portal;

import com.servengine.user.Role;

import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Transient;

@Entity
@NamedQueries({ @NamedQuery(name="Component.findAll", query="select c from Component c")
		} )
public class

Component implements java.io.Serializable, Comparable<Component>
{
//	private static org.apache.commons.logging.Log log=org.apache.commons.logging.LogFactory.getLog(Component.class.getName());
	protected String className;
	protected Set<Role> roles;
	protected boolean admin, guest;

	public Component()
	{
	}

	public Component(String className, boolean unidentifiedUserAction, boolean adminServiceAction)
	{
		this.className=className;
		this.guest=unidentifiedUserAction;
		this.admin=adminServiceAction;
	}

	public boolean equals(Object o)
	{
		return ((Component)o).getClassName().equals(getClassName());
	}

	@Transient
	public String getServiceName()
	{
		String servicename=getClassName().substring(getClassName().indexOf(".")+1); // quitamos com
		servicename=servicename.substring(servicename.indexOf(".")+1); // quitamos servengine/tuportal
		return servicename.indexOf(".")==-1?"":servicename.substring(0, servicename.indexOf("."));
	}

	@Id
	public java.lang.String getClassName()
	{
		return className;
	}

	public void setClassName(String className)
	{
		this.className=className;
	}

	public int compareTo(Component o)
	{
		return className.compareTo(o.getClassName());
	}

/*	@Transient
	public boolean isUnidentifiedUserAction()
	{
		try
		{
			for (Class myInterface: Class.forName(className).getInterfaces())//FIXME volver a probar lo comentado
				if (myInterface.getName().equals("com.servengine.struts.UnidentifiedUserAction"))
					return true;
			return false;
//		  return Class.forName("com.servengine.struts.UnidentifiedUserAction").isAssignableFrom(Class.forName(className));
//			UnidentifiedUserAction aux = (UnidentifiedUserAction)Class.forName(className).newInstance();
//			return true;
		} //catch (ClassCastException e)
		//{
		//	return false;
		//}
		catch (ClassNotFoundException e)
		{
			log.error(e.getMessage(), e);
			return false;
		}
	}

	@Transient
	public boolean isAdminServiceAction()
	{
		try
		{
		  for (Class myInterface: Class.forName(className).getInterfaces())//FIXME volver a probar lo comentado
		    if (myInterface.getName().equals("com.servengine.struts.AdminServiceAction"))
		      return true;
		  return false;

		  //return Class.forName("com.servengine.struts.AdminServiceAction").isAssignableFrom(Class.forName(className));
		} catch (ClassNotFoundException e)
		{
			log.error(e.getMessage(), e);
			return false;
		}
	}*/

	/**
	 * @deprecated Makes no sense in multiple portal site where a Component is usually used in multiple portals.
	 */
	@ManyToMany(targetEntity=Role.class, fetch=FetchType.LAZY, mappedBy="actions")
	public Set<Role> getRoles()
	{
		return roles;
	}

	/**
	 * @deprecated Makes no sense in multiple portal site where a Component is usually used in multiple portals.
	 */
	public void setRoles(Set<Role> roles)
	{
		this.roles=roles;
	}

	public void setAdmin(boolean adminServiceAction)
	{
		this.admin = adminServiceAction;
	}

	public boolean isAdmin()
	{
		return admin;
	}

	public void setGuest(boolean unidentifiedUserAction)
	{
		this.guest = unidentifiedUserAction;
	}

	public boolean isGuest()
	{
		return guest;
	}
}
